package com.wuage.interfaces;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.function.Function;

/**
 * @author Shynin
 * @version 1.0
 * @date 9/27/21 4:41 PM
 */
@Data
@Builder
@ApiModel("分页对象")
@NoArgsConstructor
@AllArgsConstructor
public class PagerVO<R> {
    @ApiModelProperty("当前页码")
    private Integer current;
    @ApiModelProperty("每页条数")
    private Integer size;
    @ApiModelProperty("总条数")
    private Integer total;
    @ApiModelProperty("总页数")
    private Integer totalPage;
    @ApiModelProperty("数据局")
    private List<R> data;

    @SuppressWarnings("all")
    public static <T, R> PagerVO<R> assembleIPager2PagerVO(IPage<T> page, FunctionList<T, R> function) {
        PagerVO<R> pagerVO = new PagerVO();
        pagerVO.setCurrent(((Long) page.getCurrent()).intValue());
        pagerVO.setSize(((Long)page.getSize()).intValue());
        pagerVO.setTotal(((Long)page.getTotal()).intValue());
        pagerVO.setTotalPage(((Long)page.getPages()).intValue());
        pagerVO.setData(function.apply(page.getRecords()));
        return pagerVO;
    }

    public interface FunctionList<T, R> {
        List<R> apply(List<T> source);
    }
}

